package com.bems.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.bems.common.UtilEnum;
import com.bems.jdbc.common.QueryHelper;
import com.bems.pojo.Group;

import com.bems.pojo.POJO;
import com.bems.pojo.Template;
import com.bems.pojo.User;

public class UserDao extends BaseDao
{
	
		/**
		 *   添加用户
		 * @param user
		 * @return 成功返回ID 0：用户存在  null：插入失败 
		 */
			public Long save(User user)
			{
				if (user.getGroup_id()==null||user.getName()==null) //初始的判断
				{
					return null;
				}
				if (!isExist(user.getAccount())||StringUtils.isEmpty(user.getAccount()))
				{
					return user.save();
				}else 
				{
					return Long.parseLong("0");
				}
				
				
			}
			
		/**
		 *  	是否存在该帐号
		 * @param account 帐号
		 * @return true：存在 false:不存在
		 */
			public boolean isExist(String account) 
			{
				if (account==null)
					return false;
				Map<String, Object> map=  new HashMap<String, Object>();
				map.put("account", account);
				User user = new User();
				if(user.get(map)!=null)//存在返回 true
					return true;
				return false;
				
			}
		
			/**
			 *  通过 分组id 取得旗下的用户 缓存
			 * @param group_id
			 * @return
			 */
			public List<User> getUsers(Long group_id)
			{
				
				String filter = "group_id = ?  and status=1 ";
				String sql = "SELECT * FROM  bems_user  WHERE " + filter  + " ORDER BY id DESC";
				return QueryHelper.query_cache("User", "group_id"+group_id, User.class, sql, group_id);
				
			}
			public List<User> getUsers(Long group_id,int page,int size)
			{
				
				String filter = "group_id = ?  ";
				String sql = "SELECT * FROM  bems_user  WHERE " + filter  + " ORDER BY id DESC   LIMIT ?,?";
			//	return QueryHelper.query_cache("User", "group_id"+group_id, User.class, sql, group_id,(page-1)*size,size);
				return QueryHelper.query( User.class, sql, group_id,(page-1)*size,size);
			}
			
	
			public static void main(String[] args) {
			
						User user = new User().get(1);
						List<Template> ts = user.Templates();
						for (Template template : ts) {
							System.out.println(template.getType());
						}
				
			}
}
